<?php

namespace app\channel\controller\sys_admin;

use app\AdminController;
use app\channel\model\ProxyLevelModel;

use think\Db;
/**
 * 代理层级
 * Class Index
 * @package app\store\controller
 */
class ProxyLevel extends AdminController
{
    //*------------------------------------------------------ */
    //-- 初始化
    /*------------------------------------------------------ */
    public function initialize()
    {
        parent::initialize();
        $this->Model = new ProxyLevelModel();
    }
    /*------------------------------------------------------ */
    //-- 首页
    /*------------------------------------------------------ */
    public function index()
    {
        $this->assign('proxyStatc',$this->Model->proxyStatc());
        $this->assign('list',$this->Model->getList());
        return $this->fetch();
    }

    /*------------------------------------------------------ */
    //-- 详情调用
    /*------------------------------------------------------ */
    public function asInfo($data) {
        $this->assign('proxy_list',$this->Model->getList());
        if (empty($data['proxy_id']) == false){
            $data['upgrade_limit_proxy'] = explode(',',$data['upgrade_limit_proxy']);
        }else{
            $data['upgrade_limit_proxy'] = [];
        }
        return $data;
    }
    /*------------------------------------------------------ */
    //-- 添加前处理
    /*------------------------------------------------------ */
    public function beforeAdd($data) {
        if (empty($data['proxy_name'])){
            return $this->error('代理层级名称不能为空.');
        }
        $where[] = ['proxy_name','=',$data['proxy_name']];
        $count = $this->Model->where($where)->count();
        if ($count > 0) return $this->error('已存在相同的代理层级名称，不允许重复添加.');
        $level = $this->Model->where('proxy_id',$data['pid'])->value('level');
        $data['level'] = $level + 1;
        $data['add_time'] = $data['update_time'] = time();
        if (empty($data['upgrade_limit_proxy'])){
            $data['upgrade_limit_proxy'] = '';
        }else{
            $data['upgrade_limit_proxy'] = join(',',$data['upgrade_limit_proxy']);
        }
        if ($data["is_join_limit"] == 1){
            if ($data['join_uplevel_goods_money_limit'] == 0 && $data['join_earnest_money_limit'] == 0 && $data['join_goods_money_limit'] == 0){
                return $this->error('开启门槛限制，必须设置一项门槛.');
            }
        }
        return $data;
    }
    /*------------------------------------------------------ */
    //-- 添加后调用
    /*------------------------------------------------------ */
    public function afterAdd($data){
        $where[] = ['pid','=',$data['pid']];
        $where[] = ['proxy_id','<>',$data['proxy_id']];
        $proxy_id = $this->Model->where($where)->value('proxy_id');
        if ($proxy_id > 0){
            $this->Model->where('proxy_id',$proxy_id)->update(['pid'=>$data['proxy_id']]);
            $this->Model->upLevel();
        }
        $this->_Log($data['proxy_id'],'添加代理层级:'.$data['proxy_name'].'，上级ID：'.$data['pid']);
        return $this->success('添加成功.',url('index'));
    }
    /*------------------------------------------------------ */
    //-- 修改前处理
    /*------------------------------------------------------ */
    public function beforeEdit($data){
        if (empty($data['proxy_name'])){
            return $this->error('代理层级名称不能为空.');
        }
        if ($data['pid'] == $data['proxy_id']){
            return $this->error('进货上级不能设置当前选项.');
        }
        $where[] = ['proxy_name','=',$data['proxy_name']];
        $where[] = ['proxy_id','<>',$data['proxy_id']];
        $count = $this->Model->where($where)->count();
        if ($count > 0) return $this->error('已存在相同的代理层级名称，不允许重复添加.');
        $data['update_time'] = time();
        if (empty($data['upgrade_limit_proxy'])){
            $data['upgrade_limit_proxy'] = '';
        }else{
            $data['upgrade_limit_proxy'] = join(',',$data['upgrade_limit_proxy']);
        }
        $fields = $this->Model->getField();
        foreach ($fields as $key=>$field){
            if (strstr($key,'_limit') && empty($data[$key])){
                $data[$key] = 0;
            }
        }

        if ($data["is_join_limit"] == 1){
            if ($data['join_uplevel_goods_money_limit'] == 0 && $data['join_earnest_money_limit'] == 0 && $data['join_goods_money_limit'] == 0){
                return $this->error('开启门槛限制，必须设置一项门槛.');
            }
        }

        $this->editLeve = false;
        $this->oldPid = 0;
        $info = $this->Model->find($data['proxy_id']);
        if ($info['pid'] != $data['pid']){
            $this->editLeve = true;
            $this->oldPid = $info['pid'];
        }
        return $data;
    }
    /*------------------------------------------------------ */
    //-- 修改后调用
    /*------------------------------------------------------ */
    public function afterEdit($data){
        if ($this->editLeve == true){
            $this->Model->where('pid',$data['proxy_id'])->update(['pid'=>$this->oldPid]);//更新原下级归属
            $where[] = ['pid','=',$data['pid']];
            $where[] = ['proxy_id','<>',$data['proxy_id']];
            $this->Model->where($where)->update(['pid'=>$data['proxy_id']]);
            $this->Model->upLevel();
        }
        $this->_Log($data['proxy_id'],'修改代理层级:'.$data['proxy_name'].'，上级：'.$data['pid']);
        return $this->success('修改成功.','reload');
    }

    /*------------------------------------------------------ */
    //-- 层级政策
    /*------------------------------------------------------ */
    public function setting()
    {
        if ($this->request->isPost()) {
            Db::startTrans();//开启事务
            $post = input('post.');
            $time = time();
            foreach ($post['purchase_setting'] as $key=>$val){
                $upData = [];
                //升级货款
                $upData['uplevel_goods_money'] = $post['uplevel_goods_money'][$key];
                $upData['uplevel_goods_money_arrive'] = $post['uplevel_goods_money_arrive'][$key];
                //end

                $upData['purchase_setting'] = $val;
                $upData['invite_setting'] = $post['invite_setting'][$key];

                $upData['is_invite'] = $post['is_invite'][$key];
                $upData['is_auto_check'] = $post['is_auto_check'][$key];

                $upData['is_join_limit'] = $post['is_join_limit'][$key] * 1;
                $upData['join_earnest_money_limit'] = $post['join_earnest_money_limit'][$key] * 1;
                $upData['join_earnest_money'] = $post['join_earnest_money'][$key] * 1;
                $upData['join_goods_money_limit'] = $post['join_goods_money_limit'][$key] * 1;
                $upData['join_goods_money'] = $post['join_goods_money'][$key] * 1;
                if (empty($post['upgrade_limit_proxy'][$key])){
                    $upData['upgrade_limit_proxy'] = '';
                }else{
                    $upData['upgrade_limit_proxy'] = join(',',$post['upgrade_limit_proxy'][$key]);
                }
                $upData['upgrade_type'] = $post['upgrade_type'][$key];
                $upData['upgrade_uplevel_goods_money_limit'] = $post['upgrade_uplevel_goods_money_limit'][$key] * 1;
                $upData['upgrade_earnest_money_limit'] = $post['upgrade_earnest_money_limit'][$key] * 1;
                $upData['upgrade_earnest_money'] = $post['upgrade_earnest_money'][$key] * 1;
                $upData['upgrade_team_limit'] = $post['upgrade_team_limit'][$key] * 1;
                $upData['upgrade_direct_push_num'] = $post['upgrade_direct_push_num'][$key] * 1;
                $upData['upgrade_team_proxy_id'] = $post['upgrade_team_proxy_id'][$key] * 1;
                $upData['upgrade_team_proxy_num'] = $post['upgrade_team_proxy_num'][$key] * 1;
                $upData['upgrade_team_all_num'] = $post['upgrade_team_all_num'][$key] * 1;
                $upData['order_first_spot_limit'] = $post['order_first_spot_limit'][$key] * 1;
                $upData['order_first_spot_money'] = $post['order_first_spot_money'][$key] * 1;
                $upData['order_first_cloud_limit'] = $post['order_first_cloud_limit'][$key] * 1;
                $upData['order_first_cloud_money'] = $post['order_first_cloud_money'][$key] * 1;
                $upData['order_first_cloud_pickup_money'] = $post['order_first_cloud_pickup_money'][$key] * 1;
                $upData['order_patch_spot_money'] = $post['order_patch_spot_money'][$key] * 1;
                $upData['order_patch_cloud_money'] = $post['order_patch_cloud_money'][$key] * 1;
                $upData['order_twice_cloud_pickup_money'] = $post['order_twice_cloud_pickup_money'][$key] * 1;
                $upData['recharge_first_money'] = $post['recharge_first_money'][$key] * 1;
                $upData['recharge_each_money'] = $post['recharge_each_money'][$key] * 1;
                $upData['update_time'] = $time;
                $res = $this->Model->where('proxy_id',$key)->update($upData);
                if ($res < 1){
                    Db::rollback();//事务回滚
                    return $this->error('保存失败，请重试.');
                }
                $proxy = $this->Model->find($key);
                $this->_Log($key,'修改代理层级政策:'.$proxy['proxy_name'].'，级别：'.$proxy['level']);
            }
            Db::commit();//事务提交
            $this->Model->cleanMemcache();
            return $this->success('保存成功.','');
        }
        $this->assign('proxy_list',$this->Model->getList());
        return $this->fetch();
    }

}

